package com.t.screenofftouch;

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;

import com.startapp.android.publish.StartAppAd;
import com.startapp.android.publish.StartAppSDK;
import com.t.screenofftouch.R.id;
import com.t.screenofftouch.service.BubbleService;

public class MainActivity extends Activity implements View.OnClickListener{
	private StartAppAd startAppAd = new StartAppAd(this);
	private Button btnUninstall;
	private Button btnClose;
	private SeekBar volumeControl;
	private ImageView imviewsize;
	private ImageButton btnicon14,btnicon15,btnicon16,btnicon17,btnicon18,btnicon19,btnicon20,btnicon21,btnicon22,btnicon23,
	btnicon24,btnicon25,btnicon26,btnicon1,btnicon2,btnicon3,btnicon4,btnicon5,btnicon6,btnicon7,btnicon8,btnicon9,btnicon10,
	btnicon11,btnicon12,btnicon13;
	private LinearLayout.LayoutParams params;
	public static SharedPreferences pref;
	public static Editor editor;
	protected static final int REQUEST_CODE_ENABLE_ADMIN = 1;
	public static boolean Flag = true; 
	private ImageButton btnRate,btnShare;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);	
		setContentView(R.layout.activity_main);
		btnicon1 = (ImageButton)findViewById(R.id.icon1);
		btnicon2 = (ImageButton)findViewById(R.id.icon2);
		btnicon3 = (ImageButton)findViewById(R.id.icon3);
		btnicon4 = (ImageButton)findViewById(R.id.icon4);
		btnicon5 = (ImageButton)findViewById(R.id.icon5);
		btnicon6 = (ImageButton)findViewById(R.id.icon6);
		btnicon7 = (ImageButton)findViewById(R.id.icon7);
		btnicon8 = (ImageButton)findViewById(R.id.icon8);
		btnicon9 = (ImageButton)findViewById(R.id.icon9);
		btnicon10 = (ImageButton)findViewById(R.id.icon10);
		btnicon11 = (ImageButton)findViewById(R.id.icon11);
		btnicon12 = (ImageButton)findViewById(R.id.icon12);
		btnicon13 = (ImageButton)findViewById(R.id.icon13);
		btnicon14 = (ImageButton)findViewById(R.id.icon14);
		btnicon15=(ImageButton)findViewById(R.id.icon15);
		btnicon16=(ImageButton)findViewById(R.id.icon16);
		btnicon17=(ImageButton)findViewById(R.id.icon17);
		btnicon18=(ImageButton)findViewById(R.id.icon18);
		btnicon19=(ImageButton)findViewById(R.id.icon19);
		btnicon20=(ImageButton)findViewById(R.id.icon20);
		btnicon21=(ImageButton)findViewById(R.id.icon21);
		btnicon22=(ImageButton)findViewById(R.id.icon22);
		btnicon23=(ImageButton)findViewById(R.id.icon23);
		btnicon24=(ImageButton)findViewById(R.id.icon24);
		btnicon25=(ImageButton)findViewById(R.id.icon25);
		btnicon26=(ImageButton)findViewById(R.id.icon26);
		
		btnicon1.setOnClickListener(this);
		btnicon2.setOnClickListener(this);
		btnicon3.setOnClickListener(this);
		btnicon4.setOnClickListener(this);
		btnicon5.setOnClickListener(this);
		btnicon6.setOnClickListener(this);
		btnicon7.setOnClickListener(this);
		btnicon8.setOnClickListener(this);
		btnicon9.setOnClickListener(this);
		btnicon10.setOnClickListener(this);
		btnicon11.setOnClickListener(this);
		btnicon12.setOnClickListener(this);
		btnicon13.setOnClickListener(this);
		btnicon14.setOnClickListener(this);
		btnicon15.setOnClickListener(this);
		btnicon16.setOnClickListener(this);
		btnicon17.setOnClickListener(this);
		btnicon18.setOnClickListener(this);
		btnicon19.setOnClickListener(this);
		btnicon20.setOnClickListener(this);
		btnicon21.setOnClickListener(this);
		btnicon22.setOnClickListener(this);
		btnicon23.setOnClickListener(this);
		btnicon24.setOnClickListener(this);
		btnicon25.setOnClickListener(this);
		btnicon26.setOnClickListener(this);
		btnShare = (ImageButton)findViewById(R.id.btnshare);
		btnShare.setOnClickListener(this);
		
		pref = getApplicationContext().getSharedPreferences("screenoff", MODE_PRIVATE); 
		editor = pref.edit();
		int w = pref.getInt("width", 0);
		if(w==0){
			writefile("width",50,0, 0);
		}
		int width = getWidth();
		volumeControl = (SeekBar) findViewById(R.id.volume_bar);
		volumeControl.setProgress(width);
		imviewsize = (ImageView)findViewById(id.imgviewsize);
		int icon = pref.getInt("icon", 0);
		if(icon==0){
			writefile("icon",4,0, 0);
		}
		loadIcon();
		if(!CommonFunctions.isMyServiceRunning(BubbleService.class, getApplicationContext())){ 
			startService(new Intent(getApplicationContext(), BubbleService.class));
		}
		params = (LinearLayout.LayoutParams) imviewsize.getLayoutParams(); 
		params.width = width;
	    params.height = width;
	    imviewsize.setLayoutParams(params);
	    btnRate = (ImageButton)findViewById(R.id.btnRate);
		btnUninstall = (Button)findViewById(R.id.btnUninstall);
		btnClose = (Button)findViewById(R.id.btnClose);
		btnClose.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		btnUninstall.setOnClickListener(btnUninstallOnClick);
		btnRate.setOnClickListener(btnRateOnClick);
		StartAppSDK.init(this, "107807268", "209053177", true);
		if(networkEnabled())
		{
			//startAppAd.showAd();
			startAppAd.loadAd();
		}
		volumeControl.setOnSeekBarChangeListener(volumeControlOnChange);
	}
	@Override
	public void onClick(View view) {
	    if (view.equals(btnicon1)){
	    	writefile("icon", 1, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon2)){
	    	writefile("icon", 2, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon3)){
	    	writefile("icon", 3, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon4)){
	    	writefile("icon", 4, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon5)){
	    	writefile("icon", 5, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon6)){
	    	writefile("icon", 6, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon7)){
	    	writefile("icon", 7, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon8)){
	    	writefile("icon", 8, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon9)){
	    	writefile("icon", 9, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon10)){
	    	writefile("icon", 10, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon11)){
	    	writefile("icon", 11, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon12)){
	    	writefile("icon", 12, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon13)){
	    	writefile("icon", 13, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon14)){
	    	writefile("icon", 14, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon15)){
	    	writefile("icon", 15, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon16)){
	    	writefile("icon", 16, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon17)){
	    	writefile("icon", 17, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon18)){
	    	writefile("icon", 18, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon19)){
	    	writefile("icon", 19, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon20)){
	    	writefile("icon", 20, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon21)){
	    	writefile("icon", 21, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon22)){
	    	writefile("icon", 22, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon23)){
	    	writefile("icon", 23, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon24)){
	    	writefile("icon", 24, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnicon25)){
	    	writefile("icon", 25, 0, 0);
	    	loadIcon();
	    }
	    else if(view.equals(btnicon26)){
	    	writefile("icon", 26, 0, 0);
	    	loadIcon();
	    }else if(view.equals(btnShare)){
	    	Intent myIntent=new Intent(this, FacebookActivity.class);
			startActivity(myIntent);
	    }
	    
	    
	        // Do something
	}
	public void loadIcon()
	{
		switch(MainActivity.geticon()){
		case 1:
			imviewsize.setImageResource(R.drawable.icon1);
			break;
		case 2:
			imviewsize.setImageResource(R.drawable.icon2);
			break;
		case 3:
			imviewsize.setImageResource(R.drawable.icon3);
			break;
		case 4:
			imviewsize.setImageResource(R.drawable.icon4);
			break;
		case 5:
			imviewsize.setImageResource(R.drawable.icon5);
			break;
		case 6:
			imviewsize.setImageResource(R.drawable.icon6);
			break;
		case 7:
			imviewsize.setImageResource(R.drawable.icon7);
			break;
		case 8:
			imviewsize.setImageResource(R.drawable.icon8);
			break;
		case 9:
			imviewsize.setImageResource(R.drawable.icon9);
			break;
		case 10:
			imviewsize.setImageResource(R.drawable.icon10);
			break;
		case 11:
			imviewsize.setImageResource(R.drawable.icon11);
			break;
		case 12:
			imviewsize.setImageResource(R.drawable.icon12);
			break;
		case 13:
			imviewsize.setImageResource(R.drawable.icon13);
			break;
		case 14:
			imviewsize.setImageResource(R.drawable.icon14);
			break;
		case 15:
			imviewsize.setImageResource(R.drawable.icon15);
			break;
		case 16:
			imviewsize.setImageResource(R.drawable.icon16);
			break;
		case 17:
			imviewsize.setImageResource(R.drawable.icon17);
			break;
		case 18:
			imviewsize.setImageResource(R.drawable.icon18);
			break;
		case 19:
			imviewsize.setImageResource(R.drawable.icon19);
			break;
		case 20:
			imviewsize.setImageResource(R.drawable.icon20);
			break;
		case 21:
			imviewsize.setImageResource(R.drawable.icon21);
			break;
		case 22:
			imviewsize.setImageResource(R.drawable.icon22);
			break;
		case 23:
			imviewsize.setImageResource(R.drawable.icon23);
			break;
		case 24:
			imviewsize.setImageResource(R.drawable.icon24);
			break;
		case 25:
			imviewsize.setImageResource(R.drawable.icon25);
			break;
		case 26:
			imviewsize.setImageResource(R.drawable.icon26);
			break;
		default:
			imviewsize.setImageResource(R.drawable.icon4);
		}
		if(CommonFunctions.isMyServiceRunning(BubbleService.class, getApplicationContext())){ 
			stopService(new Intent(this, BubbleService.class));
			startService(new Intent(this, BubbleService.class));
		}
	}
	public static  void writefile(String key,int value, int x, int y){
	    editor.putInt(key, value); 
	    if(key=="width")
	    {
	    	editor.putInt("X", x); 
	    	editor.putInt("Y", y);
	    }
	    editor.commit();
	}
	public static int getWidth(){
		return pref.getInt("width", 50);
	}
	public static int getX(){
		return pref.getInt("X", 0);
	}
	public static int getY(){
		return pref.getInt("Y", 0);
	}
	public static int geticon(){
		return pref.getInt("icon", 14);
	}
	

	private OnSeekBarChangeListener volumeControlOnChange = new OnSeekBarChangeListener() {
		
		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			
			
		}
		
		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			
		}
		
		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			int width = progress+ 0;
			writefile("width", width, getX(), getY());
			params.width = width;
			params.height = width;
			imviewsize.setLayoutParams(params);
		}
	};
	public boolean networkEnabled() {

		ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo wifiNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if (wifiNetwork != null && wifiNetwork.isConnected()) {
			return true;
		}

		NetworkInfo mobileNetwork = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		if (mobileNetwork != null && mobileNetwork.isConnected()) {
			return true;
		}

		NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
		if (activeNetwork != null && activeNetwork.isConnected()) {
			return true;
		}
		return false;
	}

	private OnClickListener btnUninstallOnClick	 = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			deActive();
			Uri packageUri = Uri.parse("package:com.t.screenofftouch");
            Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageUri);
            startActivity(uninstallIntent);
		}
	};
	
	private OnClickListener btnRateOnClick = new View.OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			launchMarket();
		}
	};
	
	private void deActive()
     {
         ComponentName devAdminReceiver = new ComponentName(getApplicationContext(), new DeviceAdminSampleReceiver().getClass().getName());
         DevicePolicyManager dpm = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
         dpm.removeActiveAdmin(devAdminReceiver);
     }
	 
	 @Override
	protected void onStop() {
		 try {
				
				if(CommonFunctions.isMyServiceRunning(BubbleService.class, getApplicationContext())){ 
					stopService(new Intent(this, BubbleService.class));
					startService(new Intent(this, BubbleService.class));
				}
			
			} catch (Exception e) {
				e.printStackTrace();
			}
		super.onStop();
	}
	private void launchMarket() {
	    Uri uri = Uri.parse("market://details?id=" + getPackageName());
	    Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
	    try {
	        startActivity(goToMarket);
	    } catch (ActivityNotFoundException e) {
	        Toast.makeText(this, R.string.couldnt_launch_market, Toast.LENGTH_LONG).show();
	    }
	}
	
	@Override
	protected void onResume() {
		startAppAd.onResume();
		super.onResume();
	}
	@Override
	public void onBackPressed() {
		startAppAd.onBackPressed();
		super.onBackPressed();
	}
}
